package service

import (
	"context"
	"go-shop-admin/internal/dao"
	"go-shop-admin/internal/model"
	"go-shop-admin/pkg/zydhlog"
	"time"
)

//写登录日志

type AuthLoginLogService struct {
	Scene   uint   `form:"scene" json:"scene" _:"comment:场景0 不明 1登录 2 操作 3删除 4 修改 "`
	Account string `form:"account" json:"account" binding:"required" _:"comment:post的登录账号"`
	//Password    string    `form:"password" json:"password" gorm:"type:varchar(255) not null;comment:post的密码 sha1加密"`
	//Captcha     string    `form:"captcha" json:"captcha" gorm:"size:10 not null;comment:post的验证码"`
	Msg         string    `form:"msg" json:"msg" _:"comment:错误信息"`
	RequestIpv4 string    `form:"request_ipv4" json:"request_ipv4" _:"comment:请求的ipv4"`
	RequestIpv6 string    `form:"request_ipv6" json:"request_ipv6" _:"comment:请求的ipv6"`
	RequestTime time.Time `form:"request_time" json:"request_time" _:"comment:请求的时间"`
}

func NewAuthLoginLogService(scene uint, account, msg, requestIpv4, requestIpv6 string) *AuthLoginLogService {
	return &AuthLoginLogService{
		Scene:       scene,
		Account:     account,
		Msg:         msg,
		RequestIpv4: requestIpv4,
		RequestIpv6: requestIpv6,
		RequestTime: time.Now(),
	}
}

// WriteAuthLoginLog 写入登录日志
func WriteAuthLoginLog(ctx context.Context, authLoginLogService *AuthLoginLogService) {
	defer wg.Done()
	//方法一：写入到redis

	//方法二：写入到mysql
	authLogLog := &model.AuthLoginLog{
		Scene:       authLoginLogService.Scene,
		Account:     authLoginLogService.Account,
		Msg:         authLoginLogService.Msg,
		RequestIpv4: authLoginLogService.RequestIpv4,
		RequestIpv6: authLoginLogService.RequestIpv6,
		RequestTime: authLoginLogService.RequestTime,
	}

	authLoginLogDao := dao.NewAuthLoginLogDao(ctx)
	if err := authLoginLogDao.AuthLoginLogCreate(authLogLog); err != nil {
		zydhlog.Error(err.Error())
	}

}
